//
//  DispatchStyleView.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/3/14.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "DispatchStyleView.h"


@interface DispatchStyleView()<UITextFieldDelegate,UITextViewDelegate>
{
    
        NSString *pid;
}

Strong UITableView *tableView;
Strong UIView *headView;



Strong UIButton *preButton;

Strong UIImageView * imageView0;
Strong UIImageView * imageView1;
Strong UIImageView * imageView2;
Strong UIImageView * imageView3;

Strong NSString * placeHoler;

//保存点击了那个
Assign int a;


@end

//网络
@implementation DispatchStyleView (NetWorking)
//获取门店列表接口
- (void)getStoreData{
   
    NSDictionary *parameters = nil;
    [[AFAppDotNetAPIClient sharedClient] POST:@"mOrgList.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
  
        if ([responseObject[@"code"] intValue] == 0) {
            self.loginModel = [[LoginModel alloc] init];
            for (NSDictionary * dic in responseObject[@"list"]) {
                [self.loginModel LoginModelWithDic:dic];
                [self.listArr addObject:self.loginModel.fMemo];
                [self.fMemoFid setObject:self.loginModel.fId forKey:self.loginModel.fMemo];
            }
        }else{
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
@end


@implementation DispatchStyleView

#pragma mark -- UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
        [UIView animateWithDuration:0.3 animations:^{

            self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 216);
            
        }];
    return YES;

}

#pragma mark --- uitextviewDelegate

-(void)textViewDidBeginEditing:(UITextView *)textView{
    
    if (self.a == 1) {
        
    }else if(self.a == 2){
      
        [UIView animateWithDuration:0.3 animations:^{
            self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(49) - UNIT_HEIGHT(470)- 216, SCREEN_WIDTH, UNIT_HEIGHT(470));
        }];
    }else{
       
        [UIView animateWithDuration:0.3 animations:^{
            self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(49) - UNIT_HEIGHT(470)- 216, SCREEN_WIDTH, UNIT_HEIGHT(470));
        }];
    }
    
}



-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (self.a == 1) {
        if ([text isEqualToString:@"\n"]) {
            [textView resignFirstResponder];
            [UIView animateWithDuration:0.3 animations:^{
                self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(49) - UNIT_HEIGHT(470), SCREEN_WIDTH, UNIT_HEIGHT(470));
            }];
            return NO;
        }
    }else if(self.a == 2){
        if ([text isEqualToString:@"\n"]) {
            [textView resignFirstResponder];
            [UIView animateWithDuration:0.3 animations:^{
                self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(49) - UNIT_HEIGHT(350), SCREEN_WIDTH, UNIT_HEIGHT(350));
            }];
            return NO;
        }
    }else{
        if ([text isEqualToString:@"\n"]) {
            [textView resignFirstResponder];
            [UIView animateWithDuration:0.3 animations:^{
                self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(49) - UNIT_HEIGHT(350), SCREEN_WIDTH, UNIT_HEIGHT(350));
            }];
            return NO;
        }
    }
    
    
    return YES;
}

-(void)textViewDidChange:(UITextView *)textView
{
    if (self.a == 1) {
    [UserModel sharedInstanced].memDisTestStr = textView.text;
    }else if (self.a == 2){
    [UserModel sharedInstanced].shopTestStr = textView.text;
    }else{
    [UserModel sharedInstanced].storeTestStr = textView.text;
    }
}


#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:self.placeHoler]) {
        textView.textColor = getColor(textColor);
 
        textView.text = @"";
    }
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@""]) {
        textView.textColor = getColor(dividerColor);
        textView.text = self.placeHoler;
    }
    return YES;
}



#pragma mark -- Action
-(void)payMentClick{
    
    if (_getDispatchClick) {
        _getDispatchClick();
    }
    
}
- (void)buttonClick:(UIButton *)sender{
    if (sender != self.preButton) {
        [sender setTitleColor:getColor(@"CB2105") forState:UIControlStateNormal];
        sender.layer.borderColor = getColor(@"CB2105").CGColor;
        [self.preButton setTitleColor:getColor(@"4E4E4E") forState:UIControlStateNormal];
        self.preButton.layer.borderColor = getColor(@"4E4E4E").CGColor;
        self.preButton = sender;
    }
    
    int a = (int)(sender.tag - 2000);
    
    self.a = a;
    
    switch (a) {
        case 0:{
            
            self.imageView2.hidden = YES;
            self.imageView1.hidden = YES;
            self.imageView3.hidden = YES;
            self.imageView0.hidden = NO;
            
            [self.footViewMember removeFromSuperview];
            [self.footStore removeFromSuperview];
            [self.footShop removeFromSuperview];
            
            
            
            if (_getMemSelfClick) {
                _getMemSelfClick();
            }
            
            [self.tableView reloadData];
            self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(150) - UNIT_HEIGHT(49), SCREEN_WIDTH, UNIT_HEIGHT(150));
            break;
                }
        case 1:{
            
            self.imageView2.hidden = YES;
            self.imageView0.hidden = YES;
            self.imageView3.hidden = YES;

            self.imageView1 = [UIImageView new];
            self.imageView1.image = [UIImage imageNamed:@"duihao"];
            self.imageView1.contentMode = UIViewContentModeScaleAspectFit;
            [_headView addSubview: self.imageView1];

            [ self.imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(sender.mas_right).offset(UNIT_WIDTH(2));
                make.top.mas_equalTo(sender.mas_top).offset(UNIT_HEIGHT(-5));
                make.width.mas_equalTo(UNIT_WIDTH(11));
                make.right.mas_equalTo(UNIT_WIDTH(8));
            }];

            [self.footStore removeFromSuperview];
            [self.footShop removeFromSuperview];
           

            [self.tableView reloadData];
            if (!self.footViewMember) {
            self.footViewMember = [[FootViewMember alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(300))];
            }
            
            self.footViewMember.personTextField.delegate = self;
            self.footViewMember.contactTextField.delegate = self;
            self.footViewMember.adressTextField.delegate = self;
            self.footViewMember.remarkView.delegate = self;
            
           // [UserModel sharedInstanced].memDisTestStr = self.footViewMember.remarkView.text;
      
            if (_getMemDispatch) {
                _getMemDispatch();
            }
            
            self.footViewMember.remarkView.text = self.placeHoler;
            
            [self.footerView addSubview:self.footViewMember];
            self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(49) - UNIT_HEIGHT(470), SCREEN_WIDTH, UNIT_HEIGHT(470));
            
             break;
            }
        case 2:{
            
            self.imageView1.hidden = YES;
            self.imageView0.hidden = YES;
            self.imageView3.hidden = YES;
            
            self.imageView2 = [UIImageView new];
            self.imageView2.image = [UIImage imageNamed:@"duihao"];
            self.imageView2.contentMode = UIViewContentModeScaleAspectFit;
            [_headView addSubview: self.imageView2];
            
            [ self.imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(sender.mas_right).offset(UNIT_WIDTH(2));
                make.top.mas_equalTo(sender.mas_top).offset(UNIT_HEIGHT(-5));
                make.width.mas_equalTo(UNIT_WIDTH(11));
                make.right.mas_equalTo(UNIT_WIDTH(8));
            }];
            
           
            [self getStoreData];
           
            [self.footViewMember removeFromSuperview];
            [self.footStore removeFromSuperview];
          

            [self.tableView reloadData];
            
            if(!self.footShop){
            self.footShop = [[FootViewShop alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(180))];
            }
            
            if (_getStoreClick) {
                _getStoreClick();
            }
            
            ws(self)
       
            //[UserModel sharedInstanced].storeTestStr = weakself.footShop.remarkText;
        
            
            self.footShop.remarkView.delegate = self;
           
            [self.footShop setGetChooseBtnClick:^{
                
                weakself.chooseView = [[qieappChooseView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) selectedHandle:^(NSString *str) {
                    NSLog(@"%@",str);
                    
                     weakself.storeFid = [weakself.fMemoFid objectForKey:str];
                   
                    [UserModel sharedInstanced].fid1 = weakself.storeFid;
                    [UserModel sharedInstanced].fMemo1 = str;
    
                    [weakself.footShop.chooseBtn setTitle:str forState:UIControlStateNormal];
                    
                }];
                
                weakself.chooseView.dataArray = weakself.listArr;
                [weakself.chooseView show];
                
                
            }];
            

            
            self.footShop.remarkView.delegate = self;
            self.footShop.remarkView.text = self.placeHoler;
            //[UserModel sharedInstanced].shopTestStr = self.footShop.remarkView.text;
            
            [self.footerView addSubview:self.footShop];
            self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(49) - UNIT_HEIGHT(350), SCREEN_WIDTH, UNIT_HEIGHT(350));
            break;

        }
        case 3:{
            self.imageView2.hidden = YES;
            self.imageView0.hidden = YES;
            self.imageView1.hidden = YES;
            
            self.imageView3 = [UIImageView new];
            self.imageView3.image = [UIImage imageNamed:@"duihao"];
            self.imageView3.contentMode = UIViewContentModeScaleAspectFit;
            [_headView addSubview: self.imageView3];
            
            [ self.imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(sender.mas_right).offset(UNIT_WIDTH(2));
                make.top.mas_equalTo(sender.mas_top).offset(UNIT_HEIGHT(-5));
                make.width.mas_equalTo(UNIT_WIDTH(11));
                make.right.mas_equalTo(UNIT_WIDTH(8));
            }];
            
            ws(self)
            [self getStoreData];
            [self.footViewMember removeFromSuperview];
            [self.footShop removeFromSuperview];
            [self.tableView reloadData];
            //self.imageView.hidden = NO;
            if(!self.footStore){
            self.footStore = [[FootViewShop alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(180))];
                 }
            if (_getStoreSelf) {
                _getStoreSelf();
            }
            
            self.footStore.remarkView.delegate = self;
   
            [self.footStore setGetChooseBtnClick:^{
                
                weakself.chooseView = [[qieappChooseView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) selectedHandle:^(NSString *str) {
                    NSLog(@"%@",str);
                    
                    weakself.storeSelfFid = [weakself.fMemoFid objectForKey:str];
                    [weakself.footStore.chooseBtn setTitle:str forState:UIControlStateNormal];
                    
                    [UserModel sharedInstanced].fid1 = weakself.storeSelfFid;
                    [UserModel sharedInstanced].fMemo1 = str;
                
                    
                }];
                
                weakself.chooseView.dataArray = weakself.listArr;
                [weakself.chooseView show];
            
            }];
            self.footStore.remarkView.delegate = self;
            self.footStore.remarkView.text = self.placeHoler;
            [self.footerView addSubview:self.footStore];
            //[UserModel sharedInstanced].storeTestStr = self.footStore.remarkView.text;
            
            self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(49) - UNIT_HEIGHT(350), SCREEN_WIDTH, UNIT_HEIGHT(350));
            break;
        }
    }

}
#pragma init --初始化
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        [self addSubview:self.bottomView];
        [self addSubview:self.tableView];
        self.placeHoler = @"如有特殊需求,请填写备注";
        
    }
    return self;
}

#pragma mark -- 显示还是不显示
//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UNIT_HEIGHT(200), SCREEN_WIDTH, SCREEN_HEIGHT - UNIT_HEIGHT(49)- UNIT_HEIGHT(200)) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.bounces = NO;
        _tableView.backgroundColor = getColor(whiteColor);
    }
    return _tableView;
}

- (UIView *)headView{
    
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, UNIT_HEIGHT(110))];
        _headView.backgroundColor = getColor(whiteColor);
        
        UIImageView * picImgeView = [UIImageView new];
        picImgeView.image = [UIImage imageNamed:@"peisongleixing拷贝"];
        picImgeView.contentMode = UIViewContentModeScaleAspectFit;
        
        UILabel * disPatchLabel = [[UILabel alloc] init];
        disPatchLabel.text = @"选择配送类型";
        disPatchLabel.font = DEF_FontSize_14;
        disPatchLabel.textColor = getColor(@"666666");
        
        UIImageView * helpImage = [UIImageView new];
        helpImage.image = [UIImage imageNamed:@"bangzhu"];
        helpImage.contentMode = UIViewContentModeScaleAspectFit;
        
        UILabel * helpLabel = [[UILabel alloc] init];
        helpLabel.text = @"帮助";
        helpLabel.font = DEF_FontSize_11;
        helpLabel.textColor = getColor(@"CCCCCC");
        
        UIView * lineView = [[UIView alloc] init];
        lineView.backgroundColor = getColor(dividerColor);
        
        [_headView sd_addSubviews:@[picImgeView,disPatchLabel,helpImage,helpLabel,lineView]];

        NSArray * array = @[@"会员自提",@"会员配送",@"门店配送",@"门店自提"];
        float wid = (SCREEN_WIDTH - 60)/4;
        for (int i = 0; i < array.count; i++) {
            UIButton * button = [[UIButton alloc] init];
            button.frame = CGRectMake(wid * i + UNIT_WIDTH(15) * (i + 1), UNIT_HEIGHT(70), wid, UNIT_HEIGHT(30));
            button.titleLabel.font = DEF_FontSize_13;
            [button setTitleColor:getColor(@"4E4E4E") forState:UIControlStateNormal];
            [button setTitle:array[i] forState:UIControlStateNormal];
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 5;
            button.layer.borderWidth = 1;
            button.layer.borderColor = getColor(@"4E4E4E").CGColor;
            button.tag = 2000 + i;
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_headView addSubview:button];
    
            if (i == 0) {
                
                self.imageView2.hidden = YES;
                self.imageView1.hidden = YES;
                self.imageView3.hidden = YES;
                
                self.imageView0 = [UIImageView new];
                self.imageView0.image = [UIImage imageNamed:@"duihao"];
                self.imageView0.contentMode = UIViewContentModeScaleAspectFit;
                [_headView addSubview: self.imageView0];
                
                [ self.imageView0 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_equalTo(button.mas_right).offset(UNIT_WIDTH(2));
                    make.top.mas_equalTo(button.mas_top).offset(UNIT_HEIGHT(-5));
                    make.width.mas_equalTo(UNIT_WIDTH(11));
                    make.right.mas_equalTo(UNIT_WIDTH(8));
                }];

                
                
                
                pid = @"1";
                self.preButton = button;
                self.preButton.layer.borderColor = getColor(@"CB2105").CGColor;
                [self.preButton setTitleColor:getColor(@"CB2105") forState:UIControlStateNormal];
              // self.footerView.frame = CGRectMake(0, 0, 0, 0);
                self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(150) - UNIT_HEIGHT(49), SCREEN_WIDTH, UNIT_HEIGHT(150));
               //self.imageView.hidden = NO;
            }
            
        }
   
        
        [picImgeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(14));
            make.left.mas_equalTo(UNIT_WIDTH(15));
            make.height.mas_equalTo(UNIT_WIDTH(30));
            make.width.mas_equalTo(UNIT_HEIGHT(30));
        }];
        
        [disPatchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(14));
            make.height.mas_equalTo(30);
            make.left.mas_equalTo(picImgeView.mas_right).offset(UNIT_WIDTH(10));
            make.centerY.mas_equalTo(picImgeView.mas_centerY);
        }];
        
        [helpLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(14));
            make.right.mas_equalTo(-11);
            make.centerY.mas_equalTo(picImgeView.mas_centerY);
            make.height.mas_equalTo(30);
        }];
        
        [helpImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(helpLabel.mas_left).offset(UNIT_WIDTH(-9));
            make.centerY.mas_equalTo(picImgeView.mas_centerY);
            make.height.mas_equalTo(15);
            make.width.mas_equalTo(15);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(1);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(picImgeView.mas_bottom).offset(UNIT_HEIGHT(15));
        }];
        
    }
    return _headView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] init];
        
        _footerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);
        
    }
    return _footerView;
}

// 底部视图

- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - UNIT_HEIGHT(49), SCREEN_WIDTH, UNIT_HEIGHT(49))];
        _bottomView.backgroundColor = [UIColor whiteColor];
        
        _shopImageview = [UIImageView new];
        _shopImageview.contentMode = UIViewContentModeScaleAspectFit;
        _shopImageview.image = [UIImage imageNamed:@"gouwuche"];
        
        _totalPriceLabel = [[UILabel alloc] init];
        _totalPriceLabel.textColor = getColor(@"EC4350");
        _totalPriceLabel.font = DEF_FontSize_18;
        _totalPriceLabel.text = @"￥999.6";
        
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.font = DEF_FontSize_14;
        _numberLabel.textColor = getColor(@"323232");
        _numberLabel.text = @"销量 :";
        
        _countLabel = [[UILabel alloc] init];
        _countLabel.font =DEF_FontSize_14;
        _countLabel.textColor = getColor(@"323232");
        _countLabel.text = @"9999";
        
        _payBtn = [[UIButton alloc] init];
        _payBtn .backgroundColor = getColor(@"EC4350");
        [_payBtn setTitle:@"确认支付" forState:UIControlStateNormal];
        [_payBtn setTitleColor:getColor(@"FFFFFF") forState:UIControlStateNormal];
        [_payBtn addTarget:self action:@selector(payMentClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_bottomView sd_addSubviews:@[_shopImageview,_totalPriceLabel,_numberLabel,_countLabel,_payBtn]];
        
        _shopImageview.sd_layout
        .topSpaceToView(_bottomView ,UNIT_HEIGHT(12))
        .leftSpaceToView(_bottomView , UNIT_HEIGHT(16))
        .widthIs(UNIT_WIDTH(30))
        .heightIs(UNIT_HEIGHT(30));
        
        _totalPriceLabel.sd_layout
        .centerYEqualToView(_shopImageview)
        .leftSpaceToView(_shopImageview , UNIT_HEIGHT(6))
        .widthIs(UNIT_WIDTH(100))
        .heightIs(UNIT_HEIGHT(30));
        
        _numberLabel.sd_layout
        .centerYEqualToView(_shopImageview)
        .leftSpaceToView(_totalPriceLabel, UNIT_HEIGHT(6))
        .widthIs(UNIT_WIDTH(50))
        .heightIs(UNIT_HEIGHT(30));
        
        _countLabel.sd_layout
        .centerYEqualToView(_shopImageview)
        .leftSpaceToView(_numberLabel, UNIT_HEIGHT(0))
        .widthIs(UNIT_WIDTH(60))
        .heightIs(UNIT_HEIGHT(30));
        
        [_payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.width.mas_equalTo(UNIT_WIDTH(100));
        }];
    }
    return _bottomView;
}

- (NSMutableArray *)listArr{
    
    if (!_listArr) {
        _listArr = @[
                     
                     ].mutableCopy;
    }
    return _listArr;
}

- (NSMutableDictionary *)fMemoFid{
    if (!_fMemoFid) {
        _fMemoFid = [NSMutableDictionary dictionary];
    }
    return _fMemoFid;
}



@end
